package com.ruoyi.yys.domain.factory;

import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Entity;
import com.ruoyi.yys.domain.ImportExportTemplate;
import com.ruoyi.yys.domain.export.DictVo;
import com.ruoyi.yys.domain.export.StyleVo;
import com.ruoyi.yys.util.XyUtils;
import com.ruoyi.yys.util.YamlUtil;
import com.ruoyi.yys.util.excel.ExcelUtils;

import java.util.List;

/**
 * sql导出
 *
 * @author lys
 * @date 2022/12/26
 */
public class FacSqlExport implements FactoryOperaty {
    private ExcelUtils excelUtils;

    private ImportExportTemplate template;

    private List<Entity> datas;

    private String id;

    private TimeInterval timer;

    private String filePath;

    /**
     * 实例化
     *
     * @param template   模板
     * @param excelUtils excelUtils工具类
     * @param datas      数据
     * @param id         id
     * @param timer      计时器
     */
    public FacSqlExport(ImportExportTemplate template, ExcelUtils excelUtils, List<Entity> datas, String id, TimeInterval timer, String filePath) {
        this.template = template;
        this.excelUtils = excelUtils;
        this.datas = datas;
        this.id = id;
        this.timer = timer;
        this.filePath = filePath;
    }

    @Override
    public void execute() {
        DictVo dictData = YamlUtil.strToDictVo(template.getDict());
        StyleVo styleData = YamlUtil.strToStyleVo(template.getStyle());
        if (ObjectUtil.isNotEmpty(dictData)) {
            //列转换
            datas = XyUtils.dictConvert(dictData.getColumn(), datas);
            //标题转换 列宽行高样式设置
            excelUtils.exportExcelThrow(id, datas, filePath, dictData, styleData, timer);
        } else {
            //生成excel
            excelUtils.exportExcel(id, datas, filePath, styleData, timer);
        }
    }
}
